package com.woniuxy.cloud.carl.user.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.car.client.CarManageClient;
import com.woniuxy.cloud.carl.car.client.CarQueryClient;
import com.woniuxy.cloud.carl.car.client.result.CarForStationDto;
import com.woniuxy.cloud.carl.user.client.UserCarClient;
import com.woniuxy.cloud.carl.user.client.param.AddUserCarParam;
import com.woniuxy.cloud.carl.user.client.result.UserCarDto;
import com.woniuxy.cloud.carl.user.common.UserResultCode;
import com.woniuxy.cloud.carl.user.infra.mapper.UserCarMapper;
import com.woniuxy.cloud.carl.user.infra.mapper.UserMapper;
import com.woniuxy.cloud.carl.user.infra.model.User;
import com.woniuxy.cloud.carl.user.infra.model.UserCar;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j

public class UserCarImpl implements UserCarClient {
    @Resource
    private CarQueryClient carQueryClient;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserCarMapper userCarMapper;

    @Override
    public Result addUserCar(AddUserCarParam param) {
        Result<CarForStationDto> result = carQueryClient.getDetail(param.getCarId());
        CarForStationDto car = result.getData();

        Assert.state(car!=null,"该车辆不存在，无法绑定");
        User user = userMapper.selectById(param.getUserId());
        Assert.state(user!=null,"无法找到该用户信息，绑定失败");
        UserCar userCar = BeanUtil.toBean(param, UserCar.class);


        userCar.setCarDesc(car.getCarDesc());//车辆描述
//判断是否已经存在默认车辆
        List<UserCar> userCars = userCarMapper.selectList(null);
        if(userCars==null||userCars.size()==0){
            userCar.setCurrentFlag(1);
        }else {
            userCar.setCurrentFlag(0);
        }
        userCar.setMileage(20000.0);
        userCar.setImage(car.getImage());
        userCar.setCarNum("渝.A123456");

        userCarMapper.insert(userCar);

        return Result.success();
    }

    @Override
    public Result<List<UserCarDto>> findAllMyCar(Long userId) {
        QueryWrapper<UserCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserCar> userCars = userCarMapper.selectList(queryWrapper);
        List<UserCarDto> userCarDtos = BeanUtil.copyToList(userCars, UserCarDto.class);


        return Result.success(userCarDtos);
    }

    @Override
    public Result defaultCar(Long userCarId) {
        UserCar userCar = userCarMapper.selectById(userCarId);
        Assert.state(userCar!=null,"暂无此条车辆绑定记录");
        Long userId = userCar.getUserId();//获得车主id
        QueryWrapper<UserCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("current_flag",1);
        UserCar beforeDefault = userCarMapper.selectOne(queryWrapper);
        if(beforeDefault!=null) {
            if (beforeDefault.getUserCarId() == userCarId) {
                throw new MallException(UserResultCode.ADDRESS_DOUBLE_DEFAULT);
            }
//以前默认地址存在的情况
            beforeDefault.setCurrentFlag(0);
            userCarMapper.updateById(beforeDefault);
            userCar.setCurrentFlag(1);
            userCarMapper.updateById(userCar);
        }else {//以前默认地址不存在的情况
            userCar.setCurrentFlag(1);
            userCarMapper.updateById(userCar);
        }
        return Result.success();
    }

    @Override
    public Result deleteCar(Long userCarId) {
        UserCar userCar = userCarMapper.selectById(userCarId);
        Assert.state(userCar!=null,"暂无此条车辆绑定记录");
        userCarMapper.deleteById(userCarId);
        return Result.success();
    }

    @Override
    @Log(value = "查询默认车辆")
    public Result<UserCarDto> findDefaultCar(Long userId) {
        QueryWrapper<UserCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("current_flag",1);
        UserCar userCar = userCarMapper.selectOne(queryWrapper);
        UserCarDto userCarDto = BeanUtil.toBean(userCar, UserCarDto.class);

        return Result.success(userCarDto);
    }
}
